<?php require_once dirname(__FILE__) . "/shared/config/config.php"; ?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="Content-type" value="text/html; charset=UTF-8" />
        <title>Index - Tesdo</title>
        <?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'head.php'; ?>
        <link href="<?php echo LINK_ROOT . DIR_LAYOUT_CSS; ?>/journal_detail.css" rel="stylesheet" />
        <meta name="viewport" content="width=device-width" />
        <?php
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_journal.php";
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_news.php";
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_journal_sub_detail.php";
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_category.php";
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_category_journal.php";
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_paper.php";
        require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . "lib_pager.php";

        $news_access = new dao_news();
        $journal_access = new dao_journal();
        $journal_sub_detail_access = new dao_journal_sub_detail();
        $category_access = new dao_category();
        $category_journal_access = new dao_category_journal();
        $paper_access = new dao_paper();
        $lib_pager = new lib_pager();

        if (!empty($_GET['id'])) {
            $journal_id = $_GET['id'];
            if (!empty($_GET['expand_year'])) {
                $expand_year = $_GET['expand_year'];
            }
            $journal = $journal_access->get_by_id($journal_id);
            $categories = $category_access->get_all_categories_by_journal_id($journal_id);
            if (!empty($categories) && count($categories) > 0) {
                $category = $category_access->get_by_id($categories[0]['CategoryID']);
                $category_journal = $category_journal_access->get_by_journal_id_and_category_id($journal_id, $category['CategoryID']);
            }
            $years = $paper_access->get_years($journal_id);
            $papers_archive = $paper_access->get_all_archive_by_journal_id($journal_id);
        } else if (!empty($_GET['cat_id']) && !empty($_GET['journal_id'])) {
            if (!empty($_GET['expand_year'])) {
                $expand_year = $_GET['expand_year'];
            }
            $category_id = $_GET['cat_id'];
            $category = $category_access->get_by_id($category_id);
            $journal_id = $_GET['journal_id'];
            $journal = $journal_access->get_by_id($journal_id);
            $years = $paper_access->get_years($journal_id);
            $category_journal = $category_journal_access->get_by_journal_id_and_category_id($journal_id, $category_id);
            $categories = $category_access->get_all_categories_by_journal_id($journal_id);
            $papers_archive = $paper_access->get_all_archive_by_journal_id($journal_id);
        } else if (!empty($_GET['p_journal_id'])) {
            if (!empty($_GET['month'])) {
                $month = $_GET['month'];
            }
            if (!empty($_GET['year'])) {
                $year = $_GET['year'];
            }
            if (!empty($_GET['expand_year'])) {
                $expand_year = $_GET['expand_year'];
            }
            $journal_id = $_GET['p_journal_id'];
            $journal = $journal_access->get_by_id($journal_id);
            $papers = $paper_access->get_all_by_journal_id_and_date($journal_id, $month, $year);
            $categories = $category_access->get_all_categories_by_journal_id($journal_id);
            $years = $paper_access->get_years($journal_id);
            $papers_archive = $paper_access->get_all_archive_by_journal_id($journal_id);
        } else if (!empty($_GET['paper_id'])) {
            $paper_id = $_GET['paper_id'];
            if (!empty($_GET['expand_year'])) {
                $expand_year = $_GET['expand_year'];
            }
            $paper = $paper_access->get_by_id($paper_id);
            $journal_id = $paper['JournalID'];
            $journal = $journal_access->get_by_id($journal_id);
            $categories = $category_access->get_all_categories_by_journal_id($journal_id);
            $years = $paper_access->get_years($journal_id);
            $papers_archive = $paper_access->get_all_archive_by_journal_id($journal_id);
        } else if (!empty($_GET['y_journal_id'])) {
            $journal_id = $_GET['y_journal_id'];
            if (!empty($_GET['expand_year'])) {
                $expand_year = $_GET['expand_year'];
            }
            $category_id = 0;
            if (!empty($_GET['cat_id'])) {
                $category_id = $_GET['cat_id'];
            }
            $category = $category_access->get_by_id($category_id);
            $journal = $journal_access->get_by_id($journal_id);
            $category_journal = $category_journal_access->get_by_journal_id_and_category_id($journal_id, $category_id);
            $categories = $category_access->get_all_categories_by_journal_id($journal_id);
            $years = $paper_access->get_years($journal_id);

            $papers_archive = $paper_access->get_all_archive_by_journal_id($journal_id);
        }
        ?>
    </head>
    <body>
        <?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'header.php'; ?>
        <!-- Body -->
        <div id="body">
            <section class="content-wrapper main-content clear-fix">

                <div class="DetailPage">
                    <hgroup class="title">
                        <h1>
                            <span id="MainContent_lblTitle"><?php echo $journal['Title']; ?></span></h1>
                        <h2></h2>
                    </hgroup>
                    <div class="LeftArea">
                        <div id="left_menu">

                            <div>
                                <table id="MainContent_dlcatList" cellspacing="0" style="border-collapse:collapse;">
                                    <tbody>
                                        <?php
                                        foreach ($categories as $i => $item) {
                                            ?>
                                            <tr>
                                                <td>
                                                    <ul>
                                                        <li><a href="journal_detail.php?cat_id=<?php echo $item['CategoryID']; ?>&journal_id=<?php echo $journal_id; ?>&expand_year=<?php
                                                            if (!empty($expand_year)) {
                                                                echo $expand_year;
                                                            }
                                                            ?>"><?php echo $item['Title']; ?></a></li>
                                                    </ul>
                                                </td>
                                            </tr>
                                            <?php
                                        }
                                        ?>

                                    </tbody></table>
                            </div>
                        </div>
                    </div>
                    <article class="CenterArea adjust_center">
                        <img src="<?php echo LINK_ROOT . DIR_SHARED_UPLOAD_JOURNAL_IMAGES . $journal['Photo']; ?>" id="MainContent_imgJournal">
                        <?php if (!empty($journal['PrintISSN'])) {
                            ?>
                            <b>Print ISSN:</b>
                            <span id="MainContent_lblPrintISSN"><?php echo $journal['PrintISSN']; ?></span><br>
                        <?php } ?>
                        <br>
                        <?php if (!empty($journal['OnlineISSN'])) { ?>
                            <b>Online ISSN:</b>
                            <span id="MainContent_lblOnlineISSN"><?php echo $journal['OnlineISSN']; ?></span><br>
                        <?php } ?>
                        <br>
                        <span id="MainContent_lblPublish"><?php // echo $journal['Publish'];                                       ?></span><br>
                        <span id="MainContent_lblShortDescription"><?php // echo $journal['ShortDescription'];                                      ?></span><br>
                        <span id="MainContent_lblImpactFactor">
                            <?php if (!empty($journal['ImpactFactor'])) { ?>
                                <strong>Impact Factor: <?php echo $journal['ImpactFactor']; ?></strong>
                            <?php } ?>
                        </span><br>
                        <br clear="All">

                        <?php
                        if (!empty($_GET['cat_id']) || !empty($_GET['id'])) {
                            if (!empty($category['Title'])) {
                                ?>
                                <h3>
                                    <span id="MainContent_lblCategory"><?php
                                        echo $category['Title'];
                                        ?></span></h3>
                                <p>
                                    <span id="MainContent_lblDescription"></span></p><?php echo $category_journal['description']; ?>
                                <?php
                            }
                        }
                        ?>

                        <?php
                        if (!empty($_GET['p_journal_id'])) {

                            $current_page = 1;
                            $page_size = 10;
                            if (!empty($_GET['p'])) {
                                $current_page = $_GET['p'];
                            }

                            $list_total = $papers;
                            $total_page = $lib_pager->get_total_page($list_total, $page_size);
                            $current_page_list = $lib_pager->get_current_page_list($page_size, $current_page, $list_total);


                            foreach ($papers as $i => $item) {
                                $param = "?paper_id=" . $item["PaperID"];
                                if (!empty($expand_year)) {
                                    $param .= "&expand_year=" . $expand_year;
                                }
                                ?>
                                <table id="MainContent_dlPapers" cellspacing="0" cellpadding="0" style="border-collapse:collapse;">
                                    <tbody><tr>
                                            <td>
                                                <div class="adsSortDetail">
                                                    <div>
                                                        <b> <a href="journal_detail.php<?php echo $param; ?>" style="color:blue; font-size:14px;"><?php echo $item["Title"]; ?></a></b><br>
                                                        <b>Author:</b> <?php echo $item["Author"]; ?> <br>
                                                        <b>Keywords:</b> <?php echo $item["Keywords"]; ?> <br>
                                                        <b>No of Papers:</b> <?php echo $item["Pages"]; ?><br>
                                                        <hr>
                                                        <b><a href="journal_detail.php<?php echo $param; ?>" style="color:blue; font-size:14px;">Abstract</a> | <a style="color:blue; font-size:14px;" target="_blank" href="layout/pdf/papers/<?php echo $item["PDFFiles"]; ?>">PDF</a></b>
                                                        <hr>
                                                    </div>
                                                </div></td>
                                        </tr>
                                    </tbody></table>
                                <?php
                            }
                            $params = "&p_journal_id=" . $journal_id . "&month=" . $month . "&year=" . $year;
                            if (!empty($expand_year)) {
                                $params .= "&expand_year=" . $expand_year;
                            }
                            ?>
                            <table>
                                <tbody>
                                    <tr>
                                        <?php for ($a = 1; $a <= $total_page; $a++) { ?>
                                            <td>
                                                <span>
                                                    <a href="journal_detail.php?p=<?php
                                                    echo $a;
                                                    echo $params;
                                                    ?>" <?php if ($current_page != $a) echo 'style="color:Black;"'; ?>><?php echo $a; ?></a>
                                                </span>
                                            </td>
                                        <?php } ?>
                                    </tr>
                                </tbody>
                            </table>
                            <?php
                        }
                        ?>

                        <?php
                        if (!empty($_GET['paper_id'])) {
                            $item = $paper;
                            ?>
                            <div id="MainContent_AbstractDeail" class="AbstractDeail">
                                <h4><span id="MainContent_lblAbsTitle"><?php echo $item["Title"]; ?></span></h4>
                                <hr>
                                <h5>Author:</h5>
                                <p> <span id="MainContent_lblAuthor"><?php echo $item["Author"]; ?> </span></p>
                                <h5>Citation:</h5>
                                <p>  <span id="MainContent_lblCitation"><?php echo $item["Citation"]; ?></span></p>
                                <h5>Keywords:</h5>
                                <p>   <span id="MainContent_lblKeywords"><?php echo $item["Keywords"]; ?></span></p>
                                <h5>Article Type:</h5>
                                <p>   <span id="MainContent_lblArticleType"><?php echo $item["ArticleType"]; ?></span></p>
                                <h5>Pages:</h5>
                                <p> <span id="MainContent_lblPages"><?php echo $item["Pages"]; ?></span></p>
                                <h5>DOI:</h5>
                                <p>  <span id="MainContent_lblDOI"><?php echo $item["DOI"]; ?></span></p>
                                <h5>Publisher:</h5>
                                <p>  <span id="MainContent_lblPublisher"><?php echo $item["Publisher"]; ?></span></p>
                                <h5>Abstract:</h5>
                                <p> <span id="MainContent_lblAbstract"><?php echo $item["Abstract"]; ?></span></p>

                                <hr>
                                <a id="MainContent_PDFFile" title="Download PDF File" href="layout/pdf/papers/<?php echo $item["PDFFiles"]; ?>" target="_blank">Download PDF File</a>

                            </div>
                            <?php
                        }
                        ?>

                    </article>
                    <aside>
                        <a href="mailto:papers@tesdo.org">
                            <input type="image" name="ctl00$MainContent$ImageButton1" id="MainContent_ImageButton1" class="btnSubmitArticle" src="<?php echo LINK_ROOT . DIR_LAYOUT_IMAGES . "SubmitArticle.png" ?>" style="width:190px;">
                        </a>


                        <div class="RightArea distance">
                            <div id="Archive">
                                <h1>Archive</h1>
                                <div>
                                    <table id="MainContent_dlArchive" cellspacing="0" style="border-collapse:collapse;">
                                        <tbody>
                                            <tr>
                                                <td>
                                                    <ul>
                                                        <?php
                                                        if (!empty($expand_year) && (!empty($_GET['id']) || !empty($_GET['cat_id']) || !empty($_GET['y_journal_id']) || !empty($_GET['p_journal_id']) || !empty($_GET['paper_id']))) {
                                                            for ($index = 0; $index < sizeof($years); $index++) {

//                                                                        $yearNowParam = "";
//                                                                        if (empty($_GET['expand_year'])) {
//                                                                            $yearNowParam = "&expand_year=".$years[$index]['year'];
//                                                                        }
//                                                                        $param .= $yearNowParam;
                                                                $param = "";
                                                                if (!empty($_GET['y_journal_id'])) {
                                                                    $param = "?y_journal_id=" . $years[$index]["JournalID"];
                                                                    if (!empty($category['CategoryID'])) {
                                                                        $param .= "&cat_id=" . $category['CategoryID'];
                                                                    }
                                                                } else if (!empty($_GET['p_journal_id'])) {
                                                                    $param = "?p_journal_id=" . $years[$index]["JournalID"] . "&year=" . $years[$index]['year'] . "&month=" . $month;
                                                                } else if (!empty($_GET['paper_id'])) {
                                                                    $param = "?paper_id=" . $paper_id;
                                                                } else if (!empty($_GET['cat_id'])) {
                                                                    $param = "?cat_id=" . $category_id . "&journal_id=" . $journal_id;
                                                                } else if (!empty($_GET['id'])) {
                                                                    $param = "?id=" . $journal_id;
                                                                }

                                                                if ($expand_year != $years[$index]['year']) {
                                                                    if (!empty($param)):
                                                                        $param .= "&expand_year=" . $years[$index]['year'];
                                                                    else :
                                                                        $param .= "?expand_year=" . $years[$index]['year'];
                                                                    endif;
                                                                }
                                                                ?>
                                                                <li>
                                                                    <a href="journal_detail.php<?php echo $param; ?>"><?php echo $years[$index]['year']; ?></a>
                                                                </li>
                                                                <?php
                                                                if ($years[$index]['year'] == $expand_year) {

                                                                    if (!empty($papers_archive) && sizeof($papers_archive) > 0) {
                                                                        foreach ($papers_archive as $i => $item) {
                                                                            if ($item['year'] == $expand_year) {
                                                                                ?>
                                                                                <li class="sub">
                                                                                    <a href="journal_detail.php?p_journal_id=<?php echo $item["JournalID"]; ?>&month=<?php echo $item['month']; ?>&year=<?php echo $item['year']; ?>&expand_year=<?php echo $expand_year; ?>"><?php echo $item['date_string']; ?>
                                                                                    </a>
                                                                                </li>
                                                                                <?php
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                            ?>

                                                            <?php
                                                        } else if (empty($expand_year)) {
                                                            foreach ($years as $i => $item) {
                                                                $param = "";
                                                                if (!empty($_GET['y_journal_id'])) {
                                                                    $param = "?y_journal_id=" . $item["JournalID"];
                                                                    if (!empty($category['CategoryID'])) {
                                                                        $param .= "&cat_id=" . $category['CategoryID'];
                                                                    }
                                                                } else if (!empty($_GET['p_journal_id'])) {
                                                                    $param = "?p_journal_id=" . $item["JournalID"] . "&year=" . $item['year'] . "&month=" . $month;
                                                                } else if (!empty($_GET['paper_id'])) {
                                                                    $param = "?paper_id=" . $paper_id;
                                                                } else if (!empty($_GET['cat_id'])) {
                                                                    $param = "?cat_id=" . $category_id . "&journal_id=" . $journal_id;
                                                                }

                                                                if (empty($param)) :
                                                                    $param .= "?id=$journal_id&expand_year=" . $item['year'];
                                                                else :
                                                                    $param .= "&expand_year=" . $item['year'];
                                                                endif;
                                                                ?>
                                                                <li>
                                                                    <a href="journal_detail.php<?php echo $param; ?>"><?php echo $item['year']; ?></a>
                                                                </li>
                                                                <?php
                                                            }
                                                        }
                                                        ?>
                                                    </ul>
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </aside>
                </div>

            </section>
        </div>
        <!-- End Body -->

        <?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php'; ?>
    </body>
</html>